package com.bflow.impl;

import com.bflow.api.IBflowCacheService;
import com.bflow.api.IBflowDefineActService;
import com.bflow.api.bo.BflowDefineActBo;
import com.bflow.api.constant.BflowConstant;
import com.bflow.commons.consts.CommConst;
import com.bflow.commons.exception.BizException;
import com.bflow.commons.service.BaseService;
import com.bflow.commons.util.StrUtil;
import com.bflow.dao.BflowDefineActDao;
import com.bflow.dao.po.BflowDefineActPo;
import com.bflow.util.AssertUtil;
import com.bflow.util.BeanMapper;
import com.bflow.util.JsonUtil;
import com.bflow.util.SysSeqUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;


/**
 * 功能：BflowDefineAct 活动节点定义-实现业务接口
 * 作者：haowen
 * 日期：2018-12-13
 */
@Service("bflowDefineActService")
public class BflowDefineActServiceImpl extends BaseService<BflowDefineActPo, BflowDefineActBo> implements IBflowDefineActService<BflowDefineActPo, BflowDefineActBo> {

	@Autowired
	private BflowDefineActDao dao;
	@Autowired
	private IBflowCacheService bflowCacheService;

	@Override
	protected BflowDefineActDao getDao() {
		return dao;
	}


	public void add(BflowDefineActBo bo) {
		bo.setId(SysSeqUtil.toIntIdStr(getMaxId(), 4));
		bo.setCreateTime(new Date());
		BflowDefineActPo po = BeanMapper.map(bo, BflowDefineActPo.class);
		if (dao.insert(po) <= 0) {
			throw new BizException("新增失败");
		}
	}

	@Override
	public void save(BflowDefineActBo bo) {
		if(StrUtil.isBlank(bo.getId())){
			add(bo);
		}else{
			bo.setUpdateTime(new Date());
			update(bo);
			removeCache(bo.getId(), bo.getKey());
		}
	}

	@Override
	public void addUserAuditAct(String key, String title, String auditUserId) throws BizException {
		AssertUtil.isTrue(existKey(null, key), "活动Key "+key+" 已存在,添加失败");
		BflowDefineActBo actBo = new BflowDefineActBo();
		actBo.setKey(key);
		actBo.setName(title);
		actBo.setAssignee(auditUserId);
		actBo.setStatus(CommConst.STATUS_Y);
		actBo.setCreateTime(new Date());
		add(actBo);
	}

	@Override
	public boolean existKey(String id, String key) throws BizException {
		BflowDefineActBo query = new BflowDefineActBo();
		query.setKey(key);
		BflowDefineActBo bo = this.getOneBy(query);
		if(bo != null)
			return bo.getId().equals(id);
		else
			return false;
	}

	@Override
	public BflowDefineActBo getUsableActById(String actId) throws BizException {
		AssertUtil.isNotNull(actId,"活动id不能为空");
		BflowDefineActBo query=new BflowDefineActBo();
		query.setId(actId);
		query.setStatus(BflowConstant.CommStatus.Y.getCode());
		BflowDefineActBo act = this.getOneBy(query);
		return act;
	}

	@Override
	public BflowDefineActBo getCacheByKey(String key) {
		String cacheKey = "actByKey:"+key;
		String val = bflowCacheService.get(cacheKey);
		if(StrUtil.isBlank(val)){
			BflowDefineActBo query=new BflowDefineActBo();
			query.setKey(key);
			BflowDefineActBo bo = this.getOneBy(query);
			if(bo != null)
				bflowCacheService.put(cacheKey, JsonUtil.toStr(bo));
			return bo;
		}
		return JsonUtil.toJavaObj(val, BflowDefineActBo.class);
	}

	@Override
	public String getCacheActName(String actId) throws BizException {
		BflowDefineActBo actBo = getCacheById(actId);
		return actBo==null?null:actBo.getName();
	}

	@Override
	public BflowDefineActBo getCacheById(String actId) throws BizException {
		String cacheKey = "actById:"+actId;
		String val = bflowCacheService.get(cacheKey);
		if(StrUtil.isBlank(val)){
			BflowDefineActBo bo = findById(actId);
			if(bo != null)
				bflowCacheService.put(cacheKey, JsonUtil.toStr(bo));
			return bo;
		}
		return JsonUtil.toJavaObj(val, BflowDefineActBo.class);
	}

	private void removeCache(String actId, String actKey){
		bflowCacheService.del("actById:"+actId);
		bflowCacheService.del("actByKey:"+actKey);
	}
}
